package org.sanelli.InterstellarGolf.Thread;

import org.sanelli.InterstellarGolf.Game;

import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;

public class GameThread extends Thread {
	private SurfaceHolder _holder;
	private Game _game;
	private boolean _running;
	
	public GameThread(SurfaceHolder holder, Game game) {
		this._holder = holder;
		this._game = game;
		this._running = false;
	}

	public void setRunning(boolean running) {
		this._running = running;
	}
	
	public void run() {
		Canvas c;
		long time = System.currentTimeMillis();
        while (_running) {
            c = null;
            try {
                c = _holder.lockCanvas(null);
                synchronized (_holder) {
                	Log.i("Thread update ",_game._world.getPlayer().toString());
                	_game.updatePhysic( 100 );
                	time = System.currentTimeMillis();
                    _game.onDraw(c);
                }
            } finally {
                // do this in a finally so that if an exception is thrown
                // during the above, we don't leave the Surface in an
                // inconsistent state
                if (c != null) {
                	_holder.unlockCanvasAndPost(c);
                }
            }
            try {
            	/*60FPS*/
				Thread.sleep(17);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				this._running=false;
			}
            
        }
	}
}
